/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.pedagogue.examination.module;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.letmeshare.pedagogue.core.entities.Party;
import org.letmeshare.pedagogue.core.module.AbstractModule;
import org.letmeshare.pedagogue.core.module.OrganizationModule;
import org.letmeshare.pedagogue.examination.Examination;
import org.letmeshare.pedagogue.examination.Result;

/**
 *
 * @author abin
 */
public class ExaminationModule extends AbstractModule {

    public static boolean createNewExaination(EntityManager em, Examination examination) {
        checkEntityManager(em);
        isNull(examination, "Examination cant be null");
        em.persist(examination);
        return false;
    }

    public static List<Examination> getAllExaminations(EntityManager em) {
        checkEntityManager(em);
        Query query = em.createQuery("SELECT X FROM EXAMINATION X");
        List<Examination> examinations = query.getResultList();
        return examinations;

    }

    public static void updateExamination(EntityManager em, Examination examination) {
        checkEntityManager(em);
        isNull(examination, "Examination cant be null");
        if (getExamination(em, examination.getId()) == null) {
            throw new IllegalArgumentException("Examination cant be found in given id " + examination.getId());
        }
        em.merge(examination);
    }

    public static Examination getExamination(EntityManager em, String id) {
        return em.find(Examination.class, id);
    }

    public static List<Examination> getAllExaminations(EntityManager em, Date startDate) {
        checkEntityManager(em);
        isNull(startDate, "Start date cant be null");
        Query query = em.createQuery("SELECT X FROM EXAMINATION X X.beginningTime=:startDate");
        query.setParameter("startDate", startDate);
        List<Examination> examinations = query.getResultList();
        return examinations;
    }

    public static List<Examination> getAllExaminations(EntityManager em, Date startDate, Date endDate) {
        checkEntityManager(em);
        isNull(startDate, "Start name cant be null");
        isNull(endDate, "Enda date cant be null");
        if (!startDate.before(endDate)) {
            throw new IllegalArgumentException("Start date must be before end date");
        }
        Query query = em.createQuery("SELECT X FROM EXAMINATION X X.beginningTime>=:startDate and X.beginningTime<=:endDate");
        List<Examination> examinations = query.getResultList();
        query.setParameter("startDate", startDate).setParameter("endDate", endDate);
        return examinations;
    }

    public static Result createResult(EntityManager em, String examinationId, String userName, float markPercentage) {
        checkEntityManager(em);
        isNull(examinationId, "Exam cant be null");
        isNull(userName, "User name cant be null");
        Examination examination = getExamination(em, examinationId);
        if (examination == null) {
            throw new IllegalArgumentException("Unable to find examination with given id");
        }
        Party party = OrganizationModule.getParty(em, userName);
        if (party == null) {
            throw new IllegalArgumentException("Unable to find Party with given id");
        }
        Result result = new Result();
        result.setExamination(examination);
        result.setCandidate(party);
        result.setScorepercentage(markPercentage);
        return createResult(em, result);

    }

    public static Result createResult(EntityManager em, Result result) {
        result.validate();
        em.persist(result);
        return result;
    }

    public static List<Result> getAllResults(EntityManager em, Examination examination) {
        checkEntityManager(em);
        isNull(examination, "Exam cant be null");
        Query q = em.createNamedQuery(Examination.GET_ALL_RESULT);
        q.setParameter("examination", examination);
        return q.getResultList();
    }

    public static List<Result> getAllResults(EntityManager em, String examinationId) {
        return getAllResults(em, getExamination(em, examinationId));
    }

    public static List<Party> getAllPassedCandidates(EntityManager em, Examination e) {
        checkEntityManager(em);
        isNull(e, "Exam cant be null");
        Query q = em.createNamedQuery(Examination.GET_ALL_PASSED_CANDIDATES);
        q.setParameter("examination", e);
        return q.getResultList();
    }

    public static List<Party> getAllFailedCandidates(EntityManager em, Examination e) {
        checkEntityManager(em);
        isNull(e, "Exam cant be null");
        Query q = em.createNamedQuery(Examination.GET_ALL_FAILED_CANDIDATES);
        q.setParameter("examination", e);
        return q.getResultList();
    }

    public static List<Result> getAllPassedResults(EntityManager em, Examination e) {
        checkEntityManager(em);
        isNull(e, "Exam cant be null");
        Query q = em.createNamedQuery(Examination.GET_ALL_PASSED_RESULT);
        q.setParameter("examination", e);
        return q.getResultList();
    }

    public static List<Result> getAllFailedResults(EntityManager em, Examination e) {
        checkEntityManager(em);
        isNull(e, "Exam cant be null");
        Query q = em.createNamedQuery(Examination.GET_ALL_FAILED_RESULT);
        q.setParameter("examination", e);
        return q.getResultList();
    }
}
